package com.theta.common.base.swagger.doc.core.executor;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;

import com.github.xiaoymin.knife4j.aggre.core.RouteResponse;

import cn.hutool.json.JSONUtil;


public class RestTemplateResponse implements RouteResponse {
	
	private ResponseEntity<Object> responseEntity;
	
	public RestTemplateResponse(ResponseEntity<Object> responseEntity) {
		this.responseEntity=responseEntity;
	}

	@Override
	public Map<String, String> getHeaders() {
		HttpHeaders headers=responseEntity.getHeaders();
		Map<String,String> headerMaps=new HashMap<>();
//		if(headers!=null && !headers.isEmpty()) {
//			headers.forEach((key,val)->{
//				headers.
//				headerMaps.put(key, val);
//			});
//			
//		}
		return headerMaps;
	}

	@Override
	public boolean success() {
		return true;
	}

	@Override
	public int getStatusCode() {
		return responseEntity.getStatusCodeValue();
	}

	@Override
	public String getContentType() {
		String type = this.responseEntity.getHeaders().getContentType().getType();
		return type!=null && !"".equals(type)?type:"application/json";
	}

	@Override
	public Long getContentLength() {
		return this.responseEntity.getHeaders().getContentLength();
	}

	@Override
	public Charset getCharsetEncoding() {
		Charset encodtype=this.responseEntity.getHeaders().getContentType().getCharset();
		return StringUtils.isEmpty(encodtype)?Charset.forName("UTF-8"):encodtype;
	}

	@Override
	public InputStream getBody() {
		
		return null;
	}

	@Override
	public String text() {
		return this.responseEntity==null || this.responseEntity.getBody()==null?"":JSONUtil.toJsonStr(this.responseEntity.getBody());
	}

}
